import pool from "@/lib/db";

const contactModule = {
  get: async (id) => {
    let rows;
    if (!id) {
      const sql = `SELECT * FROM contact LIMIT 1`;
      const [result] = await pool.query(sql);
      rows = result;
    } else {
      const sql = `SELECT * FROM contact WHERE id = ?`;
      const [result] = await pool.query(sql, [id]);
      rows = result;
    }
    return rows && rows.length > 0 ? rows[0] : null;
  },
  update: async (data) => {
    if (!data || !data.id) throw new Error("Invalid data provided for update");
    const { id, name = '', en_name = '', logo = '', cover = '', description = '', website = '', contact = '', mobile = '', phone = '', wechat = '', email = '', address = '', icp = '', meta_title = '', meta_description = ''} = data;
    const sql = `UPDATE contact SET name = ?, en_name = ?, logo = ?, cover = ?, description = ?, website = ?, contact = ?, mobile = ?, phone = ?, wechat = ?, email = ?, address = ?, icp = ?, meta_title = ?, meta_description = ? WHERE id = ?`;
    const [result] = await pool.query(sql, [name, en_name, logo, cover, description, website, contact, mobile, phone, wechat, email, address, icp, meta_title, meta_description, id]);
    return result;
  }
}

export default contactModule;